package cn.zzm.queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * springboot自动装配JMS的写法
 */
@Service
public class ProducerQueue {

    @Autowired
    private JmsTemplate jmsTemplate;

    // 发送消息，destination是发送到的队列，message是待发送的消息
    public void sendMessage(String destination, final String message){
        jmsTemplate.setPubSubDomain(false);
        jmsTemplate.convertAndSend(destination, message);
        jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session)
                    throws JMSException {
                TextMessage msg = session.createTextMessage();
                msg.setText("othre information");
                return msg;
            }
        });
    }


}
